//index.js
//获取应用实例
var util = require('../../utils/MD5.js')  
var list;
var app = getApp();
var intervarID
Page({
  data: {
    map:{
    
     lat:0,
     lng:0,
     markers:[],
     width: 35,
     height: 45,
     hasMarkers:false,
     iconPath: "image/location.png"
    },
    bumpRunTime:"",
    bumpSuspendTime:"",
    workingWaterLevel:"",
    startWaterLevel:"",
    stopWaterLevel:"",
    warningWaterLevel:"",
    bumplistinfo: [{
      icon: '/image/water-pump.png',
      text: '一号水泵',
      statustext:'正常',
      isunread: false,
      unreadNum: 0,
      id:1
    }, {
        icon: '/image/water-pump.png',
        text: '二号水泵',
        statustext: '正常',
        isunread: false,
        unreadNum: 0,
        id: 2
    }, {
        icon: '/image/pump.png',
        text: '格栅机',
        statustext: '正常',
        isunread: false,
        unreadNum: 0,
        id: 0
    }]
  },
  detailclick:function(e){
    
    var $id = e.currentTarget.dataset.id;
    console.log('detailclick onclick:'+$id);
    wx.setStorageSync('id', $id);
    wx.navigateTo({
      url: '../bumpdetail/bumpdetail'
    })
  },

  bacisclick: function (e) {
    wx.navigateTo({
      url: '../bumpbasic/bumpbasic'
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    
  },
  
  /**
 * 生命周期函数--监听页面卸载
 */
  onUnload: function () {
    clearInterval(intervarID);

  },

  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    console.log('onShow')
    var that = this

    //获取本地数据
    wx.getStorage({
      key: 'bumpdata',
      success: function (res) {
        console.log('getstoreage bumpdata:' + res.data);
        var fixworkingWaterLevel = res.data.stationInfo.workingWaterLevel * 1;
        var fixwarningWaterLevel = res.data.stationInfo.warningWaterLevel * 1;
        var fixstopWaterLevel = res.data.stationInfo.stopWaterLevel * 1;
        var fixstartWaterLevel = res.data.stationInfo.startWaterLevel *1;
        var lati = res.data.baseInfo.latitude*1;
        var long = res.data.baseInfo.longitude * 1
        that.setData({
          'map.lat': res.data.baseInfo.latitude,
          'map.lng': res.data.baseInfo.longitude,
          'map.markers': [{
            latitude: res.data.baseInfo.latitude,
            longitude: res.data.baseInfo.longitude,
            name: res.data.baseInfo.address,
            desc: res.data.baseInfo.address,
            iconPath: "../../image/location_green.png",
            width: 35,
            height: 40
          }],
          'map.hasMarkers': true,
          'bumpAddress': res.data.baseInfo.address,
          'bumpRunTime': res.data.stationInfo.bumpRunTime,
          'bumpSuspendTime': res.data.stationInfo.bumpSuspendTime,
          'workingWaterLevel': fixworkingWaterLevel.toFixed(2),
          'warningWaterLevel': fixwarningWaterLevel.toFixed(2),
          'startWaterLevel': fixstartWaterLevel.toFixed(2),
          'stopWaterLevel': fixstopWaterLevel.toFixed(2),
        });
        // checkListData(res);

        // wx.getLocation({
        //   type: 'gcj02', //返回可以用于wx.openLocation的经纬度
        //   success: function (res) {
        //     var latitude = res.latitude
        //     var longitude = res.longitude
        //     wx.openLocation({
        //       latitude: lati,
        //       longitude: long,
        //       scale: 28
        //     })
        //   }
        // })
      }
    });

    intervarID = setInterval(function () {
      var that = this;
      console.log('intervarID run:');

      var username = wx.getStorageSync('userName')
      var password = wx.getStorageSync('passWord')

      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;

      var token = timestamp + "@13f@" + username;
      token = util.hexMD5(token);
      console.log(" test timestamp:" + timestamp + " token:" + token + " username :" + username + "passWord :" + password);
      wx.request({
        url: "https://www.gzcri.cn/api/auth/refresh-sid.json",
        data: {
          grm: username,
          pass: password
        },
        header: { "content-type": "application/x-www-form-urlencoded" },
        method: "POST",
        success: function (res) {
          var responseData = res.data;
          console.log(responseData.result);
          if (res.data.result.code == -1) {
            console.log(" fail ");
            clearInterval(intervarID);
          } else {

            wx.setStorageSync('sid', responseData.data.sid)
          }
        }
      })
    }, 15 * 60 * 1000);
  },
})
